package org.tio.sitexxx.service.service.qj;


import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.checkerframework.checker.units.qual.K;
import org.tio.sitexxx.service.model.main.Demo;
import org.tio.sitexxx.service.model.main.Knowledge;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class KnowledgeService {
    public static final KnowledgeService me  = new KnowledgeService();
    public static final Knowledge dao = new Knowledge().dao();

    /**
     * 返回所有knowledge
     * @return
     */
    public List<Knowledge> getAd(){
        SqlPara sqlPara =   Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("knowledge.getAd");
        List<Knowledge> knowledges = dao.find(sqlPara);
        return knowledges;
    }

    /**
     * save
     * @param knowledge
     * @return
     */
    public boolean save(Knowledge knowledge){
        return knowledge.save();
    }

    /**
     * update
     * @param knowledge
     * @return
     */
    public boolean update(Knowledge knowledge){
        return knowledge.update();
    }
    /**
     * 删除数据
     * @param knowledge
     * @return
     */
    public boolean deleteById(Knowledge knowledge) {
        return Db.deleteById("knowledge", knowledge.getId());
    }

    /**
     *返回文章列表
     * @param kv
     * @return
     */
    public List<Knowledge> getArticle(Map<String,Object>  kv){

        SqlPara sqlPara =   Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("knowledge.getArticle",kv);
        List<Knowledge> knowledges =dao.find(sqlPara);
        return knowledges;
    }

    /**
     * 分页显示文章列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<Knowledge>getArticle(int pageNum,int pageSize){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "knowledge.getArticles";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        return Knowledge.dao.paginate(pageNum,pageSize,sqlPara);
    }

    /**
     *获取写死的一条通告
     * @return
     */
    public Knowledge getNotice(){
        String sqlid = "knowledge.getNotice";
        String db = Const.Db.TIO_SITE_MAIN;

        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        System.out.println(sqlPara.getSql());
        Record record = Db.use(db).findFirst(sqlPara);
        System.out.println(record==null);
        Knowledge knowledge=null;
        if(record!=null){
             knowledge= JSON.parseObject(JSON.toJSONString(record.getColumns()),Knowledge.class);
        }else{
            return null;
        }
        return knowledge ;
    }

    /**
     * 分页获取knowledge
     * @param pageSize pageSize
     * @param pageNum pageNum
     * @return List<Knowledge>
     */
    public Page<Knowledge>getNotices(int pageNum,int pageSize){
        String sqlid = "knowledge.getNotices";
        String db = Const.Db.TIO_SITE_MAIN;
        List<Knowledge>resultList=new ArrayList<>();
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        System.out.println(sqlPara.getSql());
        return Knowledge.dao.paginate(pageNum,pageSize,sqlPara);
       /* List<Record> record = Db.use(db).find(sqlPara);
        System.out.println("record:"+JSON.toJSONString(record));
        record.forEach((record1 -> {
            resultList.add(JSON.parseObject(JSON.toJSONString(record1.getColumns()),Knowledge.class));
        }));
        if(!resultList.isEmpty()){
            return resultList;
        }
        return null;*/
    }

    /**
     * 返回某个knowledge
     * @param map
     * @return
     */
    public Knowledge getKnowledge(Map<String,Object>map){
        String sqlid = "knowledge.getKnowledge";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv kv=new Kv().set(map);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,kv);
        System.out.println("sql:"+JSON.toJSONString(sqlPara));
        Record record = Db.use(db).findFirst(sqlPara);
        Knowledge knowledge=null;
        if(record!=null){
            knowledge= JSON.parseObject(JSON.toJSONString(record.getColumns()),Knowledge.class);
        }else{
            return null;
        }
        return knowledge ;
    }
}
